package www.com.library.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import www.com.common.library.R;


/**
 * @author wangxiao1@cjsc.com.cn
 * @since 2013-7-10
 * 默认下拉控件布局实现
 */
public class PullDownElasticImp implements IPullDownElastic {

    private View refreshView;
    private XRecyclerHeaderProgress arrowImageView;
    private int headContentHeight;
    private ProgressBar progressBar;
    private TextView tipsTextview;
    private TextView lastUpdatedTextView;
    private View lastTimeView;

    private Context mContext;

    public PullDownElasticImp(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        // 刷新视图顶端的的view
        refreshView = LayoutInflater.from(mContext).inflate(R.layout.refresh_top_item, null);

        // 指示器view
        arrowImageView = (XRecyclerHeaderProgress) refreshView.findViewById(R.id.xlistview_header_arrow);
        arrowImageView.setBorderColor(0xFFB9B9B9);
        // 刷新bar
        progressBar = (ProgressBar) refreshView.findViewById(R.id.xlistview_header_progressbar);
        // 下拉显示text
        tipsTextview = (TextView) refreshView.findViewById(R.id.xlistview_header_hint_textview);
        // 下来显示时间
        lastUpdatedTextView = (TextView) refreshView.findViewById(R.id.xlistview_header_time);
        lastTimeView = refreshView.findViewById(R.id.xlistview_header_last_time_ll);

        headContentHeight = dip2px(mContext, 60);
    }

    @Override
    public void onDrag(int distance) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getElasticLayout().getLayoutParams();
        lp.topMargin = distance;
        // 修改后刷新
        getElasticLayout().setLayoutParams(lp);
        arrowImageView.setMax(headContentHeight);
        arrowImageView.setProgress(headContentHeight + distance);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public View getElasticLayout() {
        return refreshView;
    }

    @Override
    public int getElasticHeight() {
        return headContentHeight;
    }

    @Override
    public void showArrow(int visibility) {
        arrowImageView.setVisibility(visibility);
    }

    @Override
    public void startAnimation(Animation animation) {
        arrowImageView.startAnimation(animation);
    }

    @Override
    public void clearAnimation() {
        arrowImageView.clearAnimation();
    }

    @Override
    public void showProgressBar(int visibility) {
        progressBar.setVisibility(visibility);
    }

    @Override
    public void setTips(String tips) {
        tipsTextview.setText(tips);
    }

    @Override
    public void showLastUpdate(int visibility) {
        lastTimeView.setVisibility(visibility);
    }

    public void setLastUpdateText(String text) {
        lastUpdatedTextView.setText(text);
    }

    @Override
    public void changeElasticState(int state, boolean isBack) {
    }

}
